package com.cn.wanxi.servlet.manage.product;

import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.service.IProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 通过菜系id查询当前所有产品
 */
@WebServlet("/CuisineFindById")
public class CuisineFindByIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cookstyleid = req.getParameter("id");
        String pageNum = req.getParameter("pageNum");//每一页多少条数据
        String pageSize = req.getParameter("pageSize");//当前第几页

//        封装model
        PageModel pageModel = new PageModel();
        int num = pageNum == null ? 0 : Integer.parseInt(pageNum);
        int size = pageSize == null ? 10 : Integer.parseInt(pageSize);

        pageModel.setPageNum(num);
        pageModel.setStartNum(num * (size - 1));

        ProductModel productModel = new ProductModel();
        productModel.setCookstyleid(cookstyleid == null ? 0 : Integer.parseInt(cookstyleid));
        productModel.setPageModel(pageModel);


        IProductService iProductService = new ProductServiceImpl();
        ResultModel resultModel = iProductService.findByCookStyleId(productModel);
        JSONArray jsonArray = JSONArray.fromObject(resultModel);
        resp.getWriter().println(jsonArray);
    }
}
